package org.jivesoftware.smackx.packet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smack.util.StringUtils;

public class VCard extends IQ
{
  private String avatar;
  private String emailHome;
  private String emailWork;
  private String firstName;
  private Map<String, String> homeAddr;
  private Map<String, String> homePhones = new HashMap();
  private String lastName;
  private String middleName;
  private String organization;
  private String organizationUnit;
  private Map<String, String> otherSimpleFields;
  private Map<String, String> otherUnescapableFields;
  private Map<String, String> workAddr;
  private Map<String, String> workPhones = new HashMap();

  public VCard()
  {
    this.homeAddr = new HashMap();
    this.workAddr = new HashMap();
    this.otherSimpleFields = new HashMap();
    this.otherUnescapableFields = new HashMap();
  }

  private void checkAuthenticated(Connection paramConnection, boolean paramBoolean)
  {
    if (paramConnection == null)
      throw new IllegalArgumentException("No connection was provided");
    if (!(paramConnection.isAuthenticated()))
      throw new IllegalArgumentException("Connection is not authenticated");
    if ((paramBoolean) && (paramConnection.isAnonymous()))
      throw new IllegalArgumentException("Connection cannot be anonymous");
  }

  private void copyFieldsFrom(VCard paramVCard)
  {
    if (paramVCard == null)
      paramVCard = new VCard();
    Field[] arrayOfField = VCard.class.getDeclaredFields();
    int i = arrayOfField.length;
    int j = 0;
    if (j >= i)
      return;
    Field localField = arrayOfField[j];
    if ((localField.getDeclaringClass() == VCard.class) && (!(Modifier.isFinal(localField.getModifiers()))));
    try
    {
      localField.setAccessible(true);
      localField.set(this, localField.get(paramVCard));
      ++j;
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      throw new RuntimeException("This cannot happen:" + localField, localIllegalAccessException);
    }
  }

  private void doLoad(Connection paramConnection, String paramString)
    throws XMPPException
  {
    setType(IQ.Type.GET);
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(getPacketID()));
    paramConnection.sendPacket(this);
    VCard localVCard = null;
    try
    {
      localVCard = (VCard)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
      if (localVCard == null)
        throw new XMPPException("Timeout getting VCard information", new XMPPError(XMPPError.Condition.request_timeout, "Timeout getting VCard information"));
    }
    catch (ClassCastException localClassCastException)
    {
      System.out.println("No VCard for " + paramString);
      do
      {
        copyFieldsFrom(localVCard);
        return;
      }
      while (localVCard.getError() == null);
      throw new XMPPException(localVCard.getError());
    }
  }

  public static byte[] getBytes(URL paramURL)
    throws IOException
  {
    byte[] arrayOfByte;
    File localFile = new File(paramURL.getPath());
    if (localFile.exists())
      arrayOfByte = getFileBytes(localFile);
    while (true)
    {
      return arrayOfByte;
      arrayOfByte = null;
    }
  }

  private static byte[] getFileBytes(File paramFile)
    throws IOException
  {
    BufferedInputStream localBufferedInputStream;
    Object localObject1 = null;
    try
    {
      localBufferedInputStream = new BufferedInputStream(new FileInputStream(paramFile));
    }
    finally
    {
      byte[] arrayOfByte;
      try
      {
        arrayOfByte = new byte[(int)paramFile.length()];
        if (localBufferedInputStream.read(arrayOfByte) != arrayOfByte.length)
          throw new IOException("Entire file not read");
      }
      finally
      {
        localObject1 = localBufferedInputStream;
        if (localObject1 != null)
          localObject1.close();
        throw localObject2;
        if (localBufferedInputStream != null)
          localBufferedInputStream.close();
        return arrayOfByte;
        localObject3 = finally;
      }
    }
  }

  private boolean hasContent()
  {
    int i;
    if ((!(hasNameField())) && (!(hasOrganizationFields())) && (this.emailHome == null) && (this.emailWork == null) && (this.otherSimpleFields.size() <= 0) && (this.otherUnescapableFields.size() <= 0) && (this.homeAddr.size() <= 0) && (this.homePhones.size() <= 0) && (this.workAddr.size() <= 0) && (this.workPhones.size() <= 0))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  private boolean hasNameField()
  {
    int i;
    if ((this.firstName == null) && (this.lastName == null) && (this.middleName == null))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  private boolean hasOrganizationFields()
  {
    int i;
    if ((this.organization == null) && (this.organizationUnit == null))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  private void updateFN()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    if (this.firstName != null)
      localStringBuilder.append(StringUtils.escapeForXML(this.firstName)).append(' ');
    if (this.middleName != null)
      localStringBuilder.append(StringUtils.escapeForXML(this.middleName)).append(' ');
    if (this.lastName != null)
      localStringBuilder.append(StringUtils.escapeForXML(this.lastName));
    setField("FN", localStringBuilder.toString());
  }

  public boolean equals(Object paramObject)
  {
    boolean bool;
    if (this == paramObject)
      bool = true;
    while (true)
    {
      VCard localVCard;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                return bool;
                if ((paramObject != null) && (getClass() == paramObject.getClass()))
                  break;
                bool = false;
              }
              localVCard = (VCard)paramObject;
              if (this.emailHome != null)
                if (this.emailHome.equals(localVCard.emailHome))
                  break label67;
              do
                while (true)
                  bool = false;
              while (localVCard.emailHome != null);
              if (this.emailWork != null)
                label67: if (this.emailWork.equals(localVCard.emailWork))
                  break label100;
              do
                while (true)
                  bool = false;
              while (localVCard.emailWork != null);
              if (this.firstName != null)
                label100: if (this.firstName.equals(localVCard.firstName))
                  break label133;
              do
                while (true)
                  bool = false;
              while (localVCard.firstName != null);
              label133: if (this.homeAddr.equals(localVCard.homeAddr))
                break;
              bool = false;
            }
            if (this.homePhones.equals(localVCard.homePhones))
              break;
            bool = false;
          }
          if (this.lastName != null)
            if (this.lastName.equals(localVCard.lastName))
              break label208;
          do
            while (true)
              bool = false;
          while (localVCard.lastName != null);
          if (this.middleName != null)
            label208: if (this.middleName.equals(localVCard.middleName))
              break label241;
          do
            while (true)
              bool = false;
          while (localVCard.middleName != null);
          if (this.organization != null)
            label241: if (this.organization.equals(localVCard.organization))
              break label274;
          do
            while (true)
              bool = false;
          while (localVCard.organization != null);
          if (this.organizationUnit != null)
            label274: if (this.organizationUnit.equals(localVCard.organizationUnit))
              break label307;
          do
            while (true)
              bool = false;
          while (localVCard.organizationUnit != null);
          label307: if (this.otherSimpleFields.equals(localVCard.otherSimpleFields))
            break;
          bool = false;
        }
        if (this.workAddr.equals(localVCard.workAddr))
          break;
        bool = false;
      }
      bool = this.workPhones.equals(localVCard.workPhones);
    }
  }

  public String getAddressFieldHome(String paramString)
  {
    return ((String)this.homeAddr.get(paramString));
  }

  public String getAddressFieldWork(String paramString)
  {
    return ((String)this.workAddr.get(paramString));
  }

  public byte[] getAvatar()
  {
    byte[] arrayOfByte;
    if (this.avatar == null)
      arrayOfByte = null;
    while (true)
    {
      return arrayOfByte;
      arrayOfByte = StringUtils.decodeBase64(this.avatar);
    }
  }

  public String getAvatarHash()
  {
    String str;
    byte[] arrayOfByte = getAvatar();
    if (arrayOfByte == null)
    {
      str = null;
      return str;
    }
    try
    {
      MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-1");
      localMessageDigest.update(arrayOfByte);
      str = StringUtils.encodeHex(localMessageDigest.digest());
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      localNoSuchAlgorithmException.printStackTrace();
      str = null;
    }
  }

  public String getChildElementXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    new VCardWriter(this, localStringBuilder).write();
    return localStringBuilder.toString();
  }

  public String getEmailHome()
  {
    return this.emailHome;
  }

  public String getEmailWork()
  {
    return this.emailWork;
  }

  public String getField(String paramString)
  {
    return ((String)this.otherSimpleFields.get(paramString));
  }

  public String getFirstName()
  {
    return this.firstName;
  }

  public String getJabberId()
  {
    return ((String)this.otherSimpleFields.get("JABBERID"));
  }

  public String getLastName()
  {
    return this.lastName;
  }

  public String getMiddleName()
  {
    return this.middleName;
  }

  public String getNickName()
  {
    return ((String)this.otherSimpleFields.get("NICKNAME"));
  }

  public String getOrganization()
  {
    return this.organization;
  }

  public String getOrganizationUnit()
  {
    return this.organizationUnit;
  }

  public String getPhoneHome(String paramString)
  {
    return ((String)this.homePhones.get(paramString));
  }

  public String getPhoneWork(String paramString)
  {
    return ((String)this.workPhones.get(paramString));
  }

  public int hashCode()
  {
    int j;
    int l;
    int i2;
    int i4;
    int i6;
    int i8;
    int i9;
    int i10;
    int i = 29 * (29 * (29 * (29 * this.homePhones.hashCode() + this.workPhones.hashCode()) + this.homeAddr.hashCode()) + this.workAddr.hashCode());
    if (this.firstName != null)
    {
      j = this.firstName.hashCode();
      int k = 29 * (i + j);
      if (this.lastName == null)
        break label243;
      l = this.lastName.hashCode();
      int i1 = 29 * (k + l);
      if (this.middleName == null)
        break label249;
      i2 = this.middleName.hashCode();
      int i3 = 29 * (i1 + i2);
      if (this.emailHome == null)
        break label255;
      i4 = this.emailHome.hashCode();
      int i5 = 29 * (i3 + i4);
      if (this.emailWork == null)
        break label261;
      i6 = this.emailWork.hashCode();
      int i7 = 29 * (i5 + i6);
      if (this.organization == null)
        break label267;
      i8 = this.organization.hashCode();
      i9 = 29 * (i7 + i8);
      if (this.organizationUnit == null)
        break label273;
      i10 = this.organizationUnit.hashCode();
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  return (29 * (i9 + i10) + this.otherSimpleFields.hashCode());
                  j = 0;
                }
                label243: l = 0;
              }
              label249: i2 = 0;
            }
            label255: i4 = 0;
          }
          label261: i6 = 0;
        }
        label267: i8 = 0;
      }
      label273: i10 = 0;
    }
  }

  public void load(Connection paramConnection)
    throws XMPPException
  {
    checkAuthenticated(paramConnection, true);
    setFrom(paramConnection.getUser());
    doLoad(paramConnection, paramConnection.getUser());
  }

  public void load(Connection paramConnection, String paramString)
    throws XMPPException
  {
    checkAuthenticated(paramConnection, false);
    setTo(paramString);
    doLoad(paramConnection, paramString);
  }

  public void save(Connection paramConnection)
    throws XMPPException
  {
    checkAuthenticated(paramConnection, true);
    setType(IQ.Type.SET);
    setFrom(paramConnection.getUser());
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(getPacketID()));
    paramConnection.sendPacket(this);
    Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localPacket == null)
      throw new XMPPException("No response from server on status set.");
    if (localPacket.getError() != null)
      throw new XMPPException(localPacket.getError());
  }

  public void setAddressFieldHome(String paramString1, String paramString2)
  {
    this.homeAddr.put(paramString1, paramString2);
  }

  public void setAddressFieldWork(String paramString1, String paramString2)
  {
    this.workAddr.put(paramString1, paramString2);
  }

  public void setAvatar(URL paramURL)
  {
    Object localObject = new byte[0];
    try
    {
      byte[] arrayOfByte = getBytes(paramURL);
      localObject = arrayOfByte;
      setAvatar(localObject);
      return;
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
  }

  public void setAvatar(byte[] paramArrayOfByte)
  {
    if (paramArrayOfByte == null)
      this.otherUnescapableFields.remove("PHOTO");
    while (true)
    {
      return;
      String str = StringUtils.encodeBase64(paramArrayOfByte);
      this.avatar = str;
      setField("PHOTO", "<TYPE>image/jpeg</TYPE><BINVAL>" + str + "</BINVAL>", true);
    }
  }

  public void setAvatar(byte[] paramArrayOfByte, String paramString)
  {
    if (paramArrayOfByte == null)
      this.otherUnescapableFields.remove("PHOTO");
    while (true)
    {
      return;
      String str = StringUtils.encodeBase64(paramArrayOfByte);
      this.avatar = str;
      setField("PHOTO", "<TYPE>" + paramString + "</TYPE><BINVAL>" + str + "</BINVAL>", true);
    }
  }

  public void setEmailHome(String paramString)
  {
    this.emailHome = paramString;
  }

  public void setEmailWork(String paramString)
  {
    this.emailWork = paramString;
  }

  public void setEncodedImage(String paramString)
  {
    this.avatar = paramString;
  }

  public void setField(String paramString1, String paramString2)
  {
    setField(paramString1, paramString2, false);
  }

  public void setField(String paramString1, String paramString2, boolean paramBoolean)
  {
    if (!(paramBoolean))
      this.otherSimpleFields.put(paramString1, paramString2);
    while (true)
    {
      return;
      this.otherUnescapableFields.put(paramString1, paramString2);
    }
  }

  public void setFirstName(String paramString)
  {
    this.firstName = paramString;
    updateFN();
  }

  public void setJabberId(String paramString)
  {
    this.otherSimpleFields.put("JABBERID", paramString);
  }

  public void setLastName(String paramString)
  {
    this.lastName = paramString;
    updateFN();
  }

  public void setMiddleName(String paramString)
  {
    this.middleName = paramString;
    updateFN();
  }

  public void setNickName(String paramString)
  {
    this.otherSimpleFields.put("NICKNAME", paramString);
  }

  public void setOrganization(String paramString)
  {
    this.organization = paramString;
  }

  public void setOrganizationUnit(String paramString)
  {
    this.organizationUnit = paramString;
  }

  public void setPhoneHome(String paramString1, String paramString2)
  {
    this.homePhones.put(paramString1, paramString2);
  }

  public void setPhoneWork(String paramString1, String paramString2)
  {
    this.workPhones.put(paramString1, paramString2);
  }

  public String toString()
  {
    return getChildElementXML();
  }

  private static abstract interface ContentBuilder
  {
    public abstract void addTagContent();
  }

  private class VCardWriter
  {
    private final StringBuilder sb;

    VCardWriter(, StringBuilder paramStringBuilder)
    {
      this.sb = paramStringBuilder;
    }

    private void appendAddress(, String paramString)
    {
      if (paramMap.size() > 0)
        appendTag("ADR", true, new VCard.ContentBuilder(this, paramString, paramMap)
        {
          public void addTagContent()
          {
            VCard.VCardWriter.access$1(this.this$1, this.val$code);
            Iterator localIterator = this.val$addr.entrySet().iterator();
            while (true)
            {
              if (!(localIterator.hasNext()))
                return;
              Map.Entry localEntry = (Map.Entry)localIterator.next();
              VCard.VCardWriter.access$2(this.this$1, (String)localEntry.getKey(), StringUtils.escapeForXML((String)localEntry.getValue()));
            }
          }
        });
    }

    private void appendEmail(, String paramString2)
    {
      if (paramString1 != null)
        appendTag("EMAIL", true, new VCard.ContentBuilder(this, paramString2, paramString1)
        {
          public void addTagContent()
          {
            VCard.VCardWriter.access$1(this.this$1, this.val$type);
            VCard.VCardWriter.access$1(this.this$1, "INTERNET");
            VCard.VCardWriter.access$1(this.this$1, "PREF");
            VCard.VCardWriter.access$2(this.this$1, "USERID", StringUtils.escapeForXML(this.val$email));
          }
        });
    }

    private void appendEmptyTag()
    {
      this.sb.append('<').append(paramObject).append("/>");
    }

    private void appendGenericFields()
    {
      Iterator localIterator2;
      Iterator localIterator1 = VCard.access$8(this.this$0).entrySet().iterator();
      if (!(localIterator1.hasNext()))
        localIterator2 = VCard.access$9(this.this$0).entrySet().iterator();
      while (true)
      {
        while (!(localIterator2.hasNext()))
        {
          return;
          Map.Entry localEntry1 = (Map.Entry)localIterator1.next();
          appendTag(localEntry1.getKey().toString(), StringUtils.escapeForXML((String)localEntry1.getValue()));
        }
        Map.Entry localEntry2 = (Map.Entry)localIterator2.next();
        appendTag(localEntry2.getKey().toString(), (String)localEntry2.getValue());
      }
    }

    private void appendN()
    {
      appendTag("N", true, new VCard.ContentBuilder(this)
      {
        public void addTagContent()
        {
          VCard.VCardWriter.access$2(this.this$1, "FAMILY", StringUtils.escapeForXML(VCard.access$13(VCard.VCardWriter.access$4(this.this$1))));
          VCard.VCardWriter.access$2(this.this$1, "GIVEN", StringUtils.escapeForXML(VCard.access$14(VCard.VCardWriter.access$4(this.this$1))));
          VCard.VCardWriter.access$2(this.this$1, "MIDDLE", StringUtils.escapeForXML(VCard.access$15(VCard.VCardWriter.access$4(this.this$1))));
        }
      });
    }

    private void appendOrganization()
    {
      if (VCard.access$10(this.this$0))
        appendTag("ORG", true, new VCard.ContentBuilder(this)
        {
          public void addTagContent()
          {
            VCard.VCardWriter.access$2(this.this$1, "ORGNAME", StringUtils.escapeForXML(VCard.access$11(VCard.VCardWriter.access$4(this.this$1))));
            VCard.VCardWriter.access$2(this.this$1, "ORGUNIT", StringUtils.escapeForXML(VCard.access$12(VCard.VCardWriter.access$4(this.this$1))));
          }
        });
    }

    private void appendPhones(, String paramString)
    {
      Iterator localIterator = paramMap.entrySet().iterator();
      while (true)
      {
        if (!(localIterator.hasNext()))
          return;
        appendTag("TEL", true, new VCard.ContentBuilder(this, (Map.Entry)localIterator.next(), paramString)
        {
          public void addTagContent()
          {
            VCard.VCardWriter.access$1(this.this$1, this.val$entry.getKey());
            VCard.VCardWriter.access$1(this.this$1, this.val$code);
            VCard.VCardWriter.access$2(this.this$1, "NUMBER", StringUtils.escapeForXML((String)this.val$entry.getValue()));
          }
        });
      }
    }

    private void appendTag(, String paramString2)
    {
      if (paramString2 == null);
      while (true)
      {
        return;
        appendTag(paramString1, true, new VCard.ContentBuilder(this, paramString2)
        {
          public void addTagContent()
          {
            VCard.VCardWriter.access$3(this.this$1).append(this.val$tagText.trim());
          }
        });
      }
    }

    private void appendTag(, String paramString2, String paramString3, boolean paramBoolean, VCard.ContentBuilder paramContentBuilder)
    {
      this.sb.append('<').append(paramString1);
      if (paramString2 != null)
        this.sb.append(' ').append(paramString2).append('=').append('\'').append(paramString3).append('\'');
      if (paramBoolean)
      {
        this.sb.append('>');
        paramContentBuilder.addTagContent();
        this.sb.append("</").append(paramString1).append(">\n");
      }
      while (true)
      {
        return;
        this.sb.append("/>\n");
      }
    }

    private void appendTag(, boolean paramBoolean, VCard.ContentBuilder paramContentBuilder)
    {
      appendTag(paramString, null, null, paramBoolean, paramContentBuilder);
    }

    private void buildActualContent()
    {
      if (VCard.access$1(this.this$0))
        appendN();
      appendOrganization();
      appendGenericFields();
      appendEmail(VCard.access$2(this.this$0), "WORK");
      appendEmail(VCard.access$3(this.this$0), "HOME");
      appendPhones(VCard.access$4(this.this$0), "WORK");
      appendPhones(VCard.access$5(this.this$0), "HOME");
      appendAddress(VCard.access$6(this.this$0), "WORK");
      appendAddress(VCard.access$7(this.this$0), "HOME");
    }

    public void write()
    {
      appendTag("vCard", "xmlns", "vcard-temp", VCard.access$0(this.this$0), new VCard.ContentBuilder(this)
      {
        public void addTagContent()
        {
          VCard.VCardWriter.access$0(this.this$1);
        }
      });
    }
  }
}